# encoding=utf8
import os
import warnings

import numpy as np
import pandas as pd
from sklearn.linear_model import Perceptron

if os.path.exists('./step2/result.csv'):
    os.remove('./step2/result.csv')

warnings.filterwarnings("ignore")

with open('感知机\第2关：scikit-learn感知机实践 - 癌细胞精准识别.py', encoding='utf-8') as f:
    code = f.read()
    # hash_name = ['预', '测', '正', '确', '率', '高', '于', '0', '.', '9']
    hash_name = []
    hash_count = [0]*len(hash_name)
    for i, name in enumerate(hash_name):
        if name in code:
            hash_count[i] = 1
    if np.array(hash_count).sum() == 10:
        print('切勿投机取巧！')
    else:

        # 要写入的文件在这里
        # 获取训练数据
        train_data = pd.read_csv('感知机/train_data.csv')
        # 获取训练标签
        train_label = pd.read_csv('感知机/train_label.csv')
        train_label = train_label['target']
        # 获取测试数据
        test_data = pd.read_csv('感知机/test_data.csv')

        md = Perceptron(eta0=.00001, max_iter=100000)
        md.fit(train_data, train_label)
        result = pd.Series(md.predict(test_data))

        result.to_csv("感知机/result.csv", header=["result"], index=[])

        # 获取预测标签
        df_result = pd.read_csv('感知机/result.csv')
        predict = df_result['result']
        # 获取真实标签
        df_label = pd.read_csv('感知机/test_label.csv')
        label = df_label['result']
        # 计算正确率
        acc = np.mean(predict == label)

        if acc > 0.9:
            print('预测正确率高于0.9')
        else:
            print('模型正确率为：%.3f,请修改' % acc)
